package com.demo.controller;

import com.demo.domain.User;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CommandController extends AbstractCommandController {
    
    //指定把参数封装到哪个对象
    public CommandController() {
        this.setCommandClass(User.class);
    }
    
    @Override
    protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
        //把命令对象强转成User对象
        User user= (User) command;
        ModelAndView mv = new ModelAndView();
        mv.addObject("user",user);
        mv.setViewName("index");
        return mv;
    }
    
    @Override
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
        String birthday = request.getParameter("birthday");
        if (birthday.contains("/")) {
            binder.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy/MM/dd"),true));
        }else {
            binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
        }
    }
}
